package com.ieasy.module.oa.project.web.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.ChangeCharset;
import com.ieasy.basic.util.cons.Const;
import com.ieasy.basic.util.date.DateUtils;
import com.ieasy.basic.util.poi.excel.ExcelUtil;
import com.ieasy.module.common.dto.JdlExcelDto;
import com.ieasy.module.common.web.action.BaseController;
import com.ieasy.module.oa.project.service.IProjectJdlService;
import com.ieasy.module.oa.project.web.form.ProjectJdlForm;
import com.ieasy.module.system.service.IPersonService;
import com.ieasy.module.system.web.form.PersonForm;

@Controller 
@RequestMapping("/admin/oa/jdl")
public class ProjectJdlAction extends BaseController {

	@Inject
	private IProjectJdlService jdlService ;
	
	@Inject
	private IPersonService personService ;
	
	/**
	 * 稼动率中心主页
	 * @return
	 */
	@RequestMapping("/jdl_center_main_UI.do")
	public String jdl_center_main_UI() {
		return Const.PROJECT + "jdl_center_main_UI" ;
	}
	
	/**
	 * 稼动率中心查询
	 * @param form
	 * @return
	 */
	@RequestMapping("/jdl_center.do")
	public @ResponseBody DataGrid jdl_center(ProjectJdlForm form) {
		return this.jdlService.dagagrid(form) ;
	}
	
	/**
	 * 稼动率中心查询
	 * @param form
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/doNotNeedAuth_export_jdl.do")
	public void export_jdl(ProjectJdlForm form, HttpServletResponse resp) {
		try {
			DataGrid dagagrid = this.jdlService.dagagrid(form) ;
			
			List<JdlExcelDto> objs = new ArrayList<JdlExcelDto>() ;
			
			List<ProjectJdlForm> rows = (List<ProjectJdlForm>) dagagrid.getRows() ;
			for (ProjectJdlForm j : rows) {
				JdlExcelDto dto = new JdlExcelDto() ;
				BeanUtils.copyProperties(j, dto, new String[]{
						"emp_enter_date","emp_dimission_date","dbmType","lbmType","dbmDate","lbmDate"
				});
				
				dto.setEmp_enter_date((null == j.getEmp_enter_date()? "-":DateUtils.formatYYYYMMDD(j.getEmp_enter_date()))) ;
				dto.setEmp_dimission_date((null == j.getEmp_dimission_date()? "-":DateUtils.formatYYYYMMDD(j.getEmp_dimission_date()))) ;
				dto.setDbmDate((null == j.getDbmDate()? "-":DateUtils.formatYYYYMMDD(j.getDbmDate()))) ;
				dto.setLbmDate((null == j.getLbmDate()? "-":DateUtils.formatYYYYMMDD(j.getLbmDate()))) ;
				
				objs.add(dto) ;
			}
			
			
			List<Map<String, Object>> footer = (List<Map<String, Object>>) dagagrid.getFooter() ;
			Map<String, Object> map = footer.get(0) ;
			
			/**下载*******************************************************/
			resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ;
			resp.setHeader("Content-disposition", "attachment;filename="+ChangeCharset.toISO_8859_1(form.getSearchYear()+"年稼动率")+".xlsx");
			
			Map<String, String> datas = new HashMap<String, String>() ;
			datas.put("title", form.getSearchYear()+"年稼动率") ;
			datas.put("totalNum", rows.size()+"") ;
			
			datas.put("total_work_days", map.get("total_work_days").toString()) ;
			datas.put("total_quot_work_days", map.get("total_quot_work_days").toString()) ;
			datas.put("m1", map.get("m1").toString()) ;
			datas.put("m2", map.get("m2").toString()) ;
			datas.put("m3", map.get("m3").toString()) ;
			datas.put("m4", map.get("m4").toString()) ;
			datas.put("m5", map.get("m5").toString()) ;
			datas.put("m6", map.get("m6").toString()) ;
			datas.put("m7", map.get("m7").toString()) ;
			datas.put("m8", map.get("m8").toString()) ;
			datas.put("m9", map.get("m9").toString()) ;
			datas.put("m10", map.get("m10").toString()) ;
			datas.put("m11", map.get("m11").toString()) ;
			datas.put("m12", map.get("m12").toString()) ;
			datas.put("annualJdl", map.get("annualJdl").toString()) ;
			datas.put("bzts1", map.get("bzts1").toString()) ;
			datas.put("bzts2", map.get("bzts2").toString()) ;
			datas.put("bzts3", map.get("bzts3").toString()) ;
			datas.put("bzts4", map.get("bzts4").toString()) ;
			datas.put("bzts5", map.get("bzts5").toString()) ;
			datas.put("bzts6", map.get("bzts6").toString()) ;
			datas.put("bzts7", map.get("bzts7").toString()) ;
			datas.put("bzts8", map.get("bzts8").toString()) ;
			datas.put("bzts9", map.get("bzts9").toString()) ;
			datas.put("bzts10", map.get("bzts10").toString()) ;
			datas.put("bzts11", map.get("bzts11").toString()) ;
			datas.put("bzts12", map.get("bzts12").toString()) ;
			datas.put("totalBzts", map.get("totalBzts").toString()) ;
			
			ExcelUtil.getInstance().exportObj2ExcelByTemplate(datas, "resources/excel/template/jdl.xlsx", resp.getOutputStream(), objs, JdlExcelDto.class, true) ;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**以下为我的稼动率*************************************************************************/
	
	/**
	 * 我的稼动率主页
	 * @param form
	 * @return
	 */
	@RequestMapping("/my_jdl_UI.do")
	public String my_jdl_UI(ProjectJdlForm form) {
		return Const.PROJECT + "my_jdl_UI" ;
	}
	@RequestMapping("/by_my_jdl.do")
	public @ResponseBody DataGrid by_my_jdl(ProjectJdlForm form) {
		return this.jdlService.dagagrid(form) ;
	}
	
	
	/**汇总及图表*************************************************************************/
	/**
	 * 多部门统计汇总页面
	 * @param form
	 * @return
	 */
	@RequestMapping("/jdl_dept_report_UI.do")
	public String jdl_dept_report_UI() {
		return Const.PROJECT + "report/jdl_dept_report_UI" ;
	}
	
	/**
	 * 多部门统计汇总
	 * @return
	 */
	@RequestMapping("/get_jdl_dept_report.do")
	public @ResponseBody Map<String, Object> get_jdl_dept_report(ProjectJdlForm form) {
		return this.jdlService.get_jdl_dept_report(form) ;
	}
	
	/**
	 * 多部门汇总统计，筛选不计算稼动率的人员页面
	 * @param dept_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/by_jdl_person_dialog.do")
	public String person_by_dept_datagrid_UI(String dept_id, Model model) {
		model.addAttribute("dept_id", dept_id) ;
		return Const.PROJECT + "report/by_jdl_person_dialog" ;
	}
	
	/**
	 * 不计算稼动率人员筛选,根据部门ID查询人员（多部门稼动率汇总统计页面用到）
	 * @param form
	 * @return
	 */
	@RequestMapping("/get_person_by_dept_datagrid.do")
	public @ResponseBody DataGrid get_person_by_dept_datagrid(PersonForm form) {
		return this.personService.datagrid(form) ;
	}
	
	@RequestMapping("/by_product_chart_UI")
	public String by_product_chart(ProjectJdlForm form, Model model) {
		model.addAttribute("product_dept", form) ;
		return Const.PROJECT + "report/by_product_chart_UI" ;
	}
	
	@RequestMapping("/by_dept_chart_UI")
	public String by_dept_chart_UI(ProjectJdlForm form, Model model) {
		return Const.PROJECT + "report/by_dept_chart_UI" ;
	}
	
}
